{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<form class="layui-form gg-form-bar border-t border-x">

    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="name" placeholder="客户编码/客户名称/单号" class="layui-input" autocomplete="off" />
    </div>
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="departName" placeholder="销售单位" class="layui-input" autocomplete="off" />
    </div>

    <div class="layui-input-inline" style="width:300px;">
        <select name="status" >
            <option value="">所有状态</option>
            {foreach ['','草稿','已确认']  as $key=>  $v}
            <option value="{$key}">{$v}</option>
            {/foreach}
        </select>
    </div>
<!--    <div class="layui-input-inline" style="width:300px;">-->
<!--        <input type="text" name="orderDate" placeholder="单据日期" readonly id="orderDate" class="layui-input" autocomplete="off" />-->
<!--    </div> -->
    <div class="layui-input-inline" style="width:300px;">
        <input type="text" name="payDate" placeholder="收款日期" readonly id="payDate" class="layui-input" autocomplete="off" />
    </div>
    <div class="layui-input-inline" style="width:150px;">
        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webform"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
        <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
    </div>
</form>
<div class="p-3">
    <div>
        <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">+ 添加收款</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger " lay-event="dels" type="button">删除</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary " lay-event="htdk" type="button">合同到款</button>
    </div>
</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script>
    const moduleInit = ['tool'];

    let statuss=['','草稿','已确认'];
    let payTypes=['','现金','银行电汇','电子承兑'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool;
        var form =layui.form;
        layui.laydate.render({
            elem:"#payDate",
            range:'~'
        })
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "index"
                ,toolbar: '#toolbarDemo'
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            // ,defaultToolbar:  ['filter',  'exports']
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                , {field: 'orderNum', title: '单据编号'}
                , {field: 'pName',  title: '客户名称'}
                , {field: 'saleUnitName',  title: '销售单位'}
                , {field: 'money',  title: '收款金额'}
                , {field: 'contactMoney',  title: '合同金额', templet: function (d) {
                        var html = '' +
                            '<a href="javascript:" class="" lay-event="seeMoneyMx">'+d.contactMoney+'</a>' +
                            '';
                        return html;
                    }}
                , {field: 'payType',  title: '收款方式',templet:function (d) {
                        return payTypes[d.payType];
                    }}
                // , {field: 'payBank',  title: '收款银行'}
                , {field: 'payName',  title: '收款人'}
                // , {field: 'orderDate',  title: '单据日期'}
                , {field: 'payDate',  title: '收款日期'}
                , {field: 'status',  title: '状态',templet:function (d) {
                    return statuss[d.status];
                    }}
                , {field: 'moRemark',  title: '备注'}
                , {title: '操作', align: 'center'
                    , templet: function (d) {
                        var html = '' +
                            '<button class="layui-btn layui-btn-normal  layui-btn-xs" lay-event="edit">编辑</button>' +
                            '';
                        return html;
                    }
                }
            ]]
            , page: true
        });
        //表头工具栏事件
        $('.add-menu').on('click', function () {
            tool.side("add");
            return;
        });
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {
            console.log(obj);
            if (obj.event === 'add') {
                tool.side('add?pid=' + obj.data.id);
                return;
            }
            if (obj.event === 'edit') {
                tool.side('add?id=' + obj.data.id);
                return;
            }
            if (obj.event === 'seeMoneyMx') {
                tool.side('dkMx?receiptId=' + obj.data.id);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            obj.del();
                        }
                    }
                    tool.delete("delete", {id: obj.data.id}, callback);
                    layer.close(index);
                });
            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    tool.side("{:url('add')}");
                    break;
                    case 'htdk':
                        var data = checkStatus.data;
                        var ids=[]
                        if (data.length){
                            data.forEach(function (value,index) {
                                console.log('value',value)
                                ids.push(value.id)
                            })
                           if (ids.length!=1){
                               layer.msg('只能选择一条数据');
                               return;
                           }
                            tool.side("{:url('htdk')}?receiptId="+ids.join(','));
                        }else {
                            layer.msg('请选择数据');
                        }

                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("{:url('delete')}", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;
                case 'plqr':

                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        $.ajax({
                            url:"{:url('onSure')}",
                            type:'post',
                            dataType:'json',
                            data:{id:ids.join(',')},
                            success:function (res) {
                                console.log('res',res)
                                layer.msg(res.msg)
                                if (res.code == 0) {
                                }
                            },
                            error(e){
                                layer.msg('网络错误，请稍后重试')
                            }
                        })
                    }else {
                        layer.msg('请选择数据');
                    }
                    break;
            };
        })
    }
</script>
{/block}
<!-- /脚本 -->